﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;

#if !SILVERLIGHT
namespace UIT.WPF.Renderers
#else
namespace UIT.Silverlight.Renderers
#endif
{
#if !SILVERLIGHT
    class WrapPanelRenderer : 
        WpfRenderer<Controls.Containers.WrapPanel, WrapPanel>
#else
    public class WrapPanelRenderer :
        SlRenderer<Controls.Containers.WrapPanel, WrapPanel>
#endif
    {
        public override void Render(Controls.Containers.WrapPanel control)
        {
            Result = new WrapPanel();
            Result.DataContext = control;
            Result.SetBinding(WrapPanel.OrientationProperty, new Binding("Orientation"));
            control.Children.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Children_CollectionChanged);
            foreach (var child in control.Children)
            {
                var childUI = Manager.Render(child);
                if (childUI != null)
                    Result.Children.Add(childUI);
            }
        }

        void Children_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    foreach (UIT.Controls.IControl  child in e.NewItems)
                    {
                        var childUI = Manager.Render(child);
                        if (childUI != null)
                            Result.Children.Add(childUI);
                    }
                    break;
#if !SILVERLIGHT
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    throw new NotSupportedException();
#endif
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    throw new NotSupportedException();
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                    throw new NotSupportedException();
                case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                    throw new NotSupportedException();
                default:
                    throw new NotSupportedException();
            }
        }
    }
}
